package com.trungsi.profile;

import org.aspectj.lang.JoinPoint.StaticPart;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public abstract class StaticProfilingAspect {

	private static TracePrinter printer = loadPrinter();
	private static Tracer tracer = new SimpleTracer(printer);
	
	@Before("pointcutDef()")
	public void beforeAdvice(StaticPart staticPart) throws Throwable {
		tracer.traceBegin(staticPart);
	}
	
	private static TracePrinter loadPrinter() {
		String printerClass = System.getProperty("profile.printer.class", SimpleTracePrinter.class.getName());
		try {
			Class<?> klass = Class.forName(printerClass);
			return (TracePrinter) klass.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@After("pointcutDef()")
	public void afterAdvice(/*StaticPart staticPart*/) throws Throwable {
		tracer.traceEnd(/*staticPart*/(StaticPart) null);
	}
	
	@Pointcut
	public abstract void pointcutDef();
}
